<?php
/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-4-5
 */
/*Database  connection  Class
 * For connecting database and gets the results Purpose
 */

class conn{
	private $sql;
	private  $line;
	private $conn;
	private $result;
	function __construct(){
		include 'Errmsg.php';
		include 'config.php';
		
			try{ $mysqli=new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
			}
			catch(Exception $err){
				echo $err->getMessage();
			}
			$this->set_conn($mysqli);
		
	}
	private function do_result()
	//return the result of the sql
	{
		$sql=$this->get_sql();
		$db=$this->get_conn();
	  $db->set_charset("utf8"); // set charset with utf8
		$result= $db->query($sql);
		$this->set_result($result);
		// $line1=$result->num_rows;
	$line1=$db->affected_rows;

		if(!empty($line1))
		{
			if($line1==-1)
			{
				$line1=0;
			}
			$this->set_line($line1);

		}
		else
		{
			//   $line=$db->affected_rows;

    	$line=$result->num_rows;

			$this->set_line($line);
		}
		$this->do_close();
	}
	private function do_close()
	// close the connection & result
	{
		$db= $this->get_conn();
		// $result->free();
		$db->close();
	}
	/*
	 * functions for get result
	 */
	private  function set_line($line)
	{
		$this->line=$line;
	}
	function set_sql($sql)
	{
		$this->sql=$sql;
	}
	private  function set_conn($conn)
	{
		$this->conn=$conn;
	}
	private function set_result($result){
		$this->result=$result;
	}
	function get_line()
	{
		$this->do_result();
		return $this->line;
	}
	private function get_sql()
	{
		return $this->sql;
	}
	private function get_conn()
	{
		return $this->conn;
	}

	function get_result()
	{
		return $this->result;
	}


}

?>